package drds.binlog.parse.driver.packets.server;

import drds.binlog.common.Authors;
import drds.binlog.parse.driver.packets.PacketWithHeaderPacket;
import drds.binlog.parse.driver.utils.Bytes;
import drds.binlog.parse.driver.utils.LengthCodedStringReader;
import drds.common.Author;

import java.io.IOException;

@Author(name = Authors.LI_YANG)
public class FieldPacket extends PacketWithHeaderPacket
{

    private String catalogName;
    private String dataBaseName;
    //
    private String tableName;
    private String originalTableName;
    //
    private String columnName;
    private String originalColumnName;
    //
    private int character;
    private long length;
    private byte type;
    private int flags;
    private byte decimals;
    private String definition;

    /**
     * <pre>
     *  VERSION 4.1
     *  Bytes                      Name
     *  -----                      ----
     *  n (Length Coded String)    catalogName
     *  n (Length Coded String)    dataBaseName
     *  n (Length Coded String)    tableName
     *  n (Length Coded String)    org_table
     *  n (Length Coded String)    columnName
     *  n (Length Coded String)    org_name
     *
     *
     *  1                          (filler)
     *  2                          charsetnr
     *  4                          length
     *  1                          type
     *  2                          flags
     *  1                          decimals
     *  2                          (filler), always 0x00
     *  n (Length Coded Binary)    default
     *
     * </pre>
     */
    public void fromBytes(byte[] bytes) throws IOException
    {

        int index = 0;
        LengthCodedStringReader lengthCodedStringReader = new LengthCodedStringReader(index, null);
        // 1.
        this.catalogName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.dataBaseName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.tableName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.originalTableName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.columnName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.originalColumnName = lengthCodedStringReader.readLengthCodedString(bytes);
        //
        index = lengthCodedStringReader.getStartIndex();//从里面获取对应的index
        //
        index++;//(filler)
        //
        this.character = Bytes.readUnsigned2IntLittleEndian(bytes, index);
        index += 2;
        //
        this.length = Bytes.readUnsigned4IntLittleEndian(bytes, index);
        index += 4;
        //
        this.type = bytes[index];
        index++;
        //
        this.flags = Bytes.readUnsigned2IntLittleEndian(bytes, index);
        index += 2;
        //
        this.decimals = bytes[index];
        index++;
        //
        index += 2;// skip filter
        //
        if (index < bytes.length)
        {
            lengthCodedStringReader.setStartIndex(index);//new start index
            this.definition = lengthCodedStringReader.readLengthCodedString(bytes);
        }
    }

    public byte[] toBytes() throws IOException
    {
        return null;
    }

    public String getCatalogName()
    {
        return catalogName;
    }

    public void setCatalogName(String catalogName)
    {
        this.catalogName = catalogName;
    }

    public String getDataBaseName()
    {
        return dataBaseName;
    }

    public void setDataBaseName(String dataBaseName)
    {
        this.dataBaseName = dataBaseName;
    }

    public String getTableName()
    {
        return tableName;
    }

    public void setTableName(String tableName)
    {
        this.tableName = tableName;
    }

    public String getOriginalTableName()
    {
        return originalTableName;
    }

    public void setOriginalTableName(String originalTableName)
    {
        this.originalTableName = originalTableName;
    }

    public String getColumnName()
    {
        return columnName;
    }

    public void setColumnName(String columnName)
    {
        this.columnName = columnName;
    }

    public String getOriginalColumnName()
    {
        return originalColumnName;
    }

    public void setOriginalColumnName(String originalColumnName)
    {
        this.originalColumnName = originalColumnName;
    }

    public int getCharacter()
    {
        return character;
    }

    public void setCharacter(int character)
    {
        this.character = character;
    }

    public long getLength()
    {
        return length;
    }

    public void setLength(long length)
    {
        this.length = length;
    }

    public byte getType()
    {
        return type;
    }

    public void setType(byte type)
    {
        this.type = type;
    }

    public int getFlags()
    {
        return flags;
    }

    public void setFlags(int flags)
    {
        this.flags = flags;
    }

    public byte getDecimals()
    {
        return decimals;
    }

    public void setDecimals(byte decimals)
    {
        this.decimals = decimals;
    }

    public String getDefinition()
    {
        return definition;
    }

    public void setDefinition(String definition)
    {
        this.definition = definition;
    }


}
